package com.mes.cloud.controller;

import com.alibaba.fastjson.JSON;
import com.mes.cloud.service.BcCRUDService;
import com.mes.cloud.util.BcParam;
import com.mes.cloud.util.Constants;
import com.mes.cloud.util.JSONResult;
import com.mes.cloud.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: cloud
 * @Package: com.mes.cloud.controller
 * @ClassName: BCController
 * @Author: xhy
 * @Description: BC控制层
 * @Date: 2019/4/24 18:29
 * @Version: 1.0
 */
@Api(tags = "BcController", description = "BC配置管理")
@RestController
@RequestMapping("/bme")
public class BcController {
    /**
     * 日志记录
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(BcController.class);

    @Autowired
    private BcCRUDService bcService;

    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "通用的del数据信息")
    @RequestMapping(value = "/{bcCode}/delete", method = RequestMethod.POST)
    public JSONResult delete(@RequestBody BcParam param,@PathVariable String bcCode) {
        param.setBcCode(bcCode);
        LOGGER.info("Class BcController:Method delete:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.delete(param.getBcCode(), param.getIds());
        LOGGER.info("Class BcController:Method delete:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @param bcCode
     * @return
     */
    @ApiOperation(value = "查询我的")
    @RequestMapping(value = "/{bcCode}/my", method = RequestMethod.POST)
    public JSONResult my(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method my:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.my(map);
        LOGGER.info("Class BcController:Method my:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @param bcCode
     * @return
     */
    @ApiOperation(value = "查询我组织的")
    @RequestMapping(value = "/{bcCode}/myOrg", method = RequestMethod.POST)
    public JSONResult myOrg(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method myOrg:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.myOrg(map);
        LOGGER.info("Class BcController:Method myOrg:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @param bcCode
     * @return
     */
    @ApiOperation(value = "查询我团队的")
    @RequestMapping(value = "/{bcCode}/myTeam", method = RequestMethod.POST)
    public JSONResult myTeam(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method myTeam:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.myTeam(map);
        LOGGER.info("Class BcController:Method myTeam:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @param bcCode
     * @return
     */
    @ApiOperation(value = "查询共享给我的")
    @RequestMapping(value = "/{bcCode}/myShare", method = RequestMethod.POST)
    public JSONResult myShare(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method myShare:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.myShare(map);
        LOGGER.info("Class BcController:Method myShare:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "通用的upd数据信息")
    @RequestMapping(value = "/{bcCode}/update", method = RequestMethod.POST)
    public JSONResult update(@RequestBody BcParam param,@PathVariable String bcCode) {
        param.setBcCode(bcCode);
        LOGGER.info("Class BcController:Method update:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.update(param.getBcCode(), param.getParam());
        LOGGER.info("Class BcController:Method update:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }


    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "多条件更新")
    @RequestMapping(value = "/{bcCode}/updateByCondition", method = RequestMethod.POST)
    public JSONResult updateByCondition(@RequestBody BcParam param,@PathVariable String bcCode) {
        param.setBcCode(bcCode);
        LOGGER.info("Class BcController:Method updateByCondition:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.updateByCondition(param.getBcCode(), param.getParam());
        LOGGER.info("Class BcController:Method updateByCondition:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }


    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "通用的批量更新数据信息")
    @RequestMapping(value = "/{bcCode}/batchUpdate", method = RequestMethod.POST)
    public JSONResult batchUpdate(@RequestBody BcParam param,@PathVariable String bcCode) {
        param.setBcCode(bcCode);
        LOGGER.info("Class BcController:Method batchUpdate:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.batchUpdate(param.getBcCode(), param.getParam(),param.getIds());
        LOGGER.info("Class BcController:Method batchUpdate:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param
     * @param
     * @return
     */
    @ApiOperation(value = "通用的新增数据信息")
    @RequestMapping(value = "/{bcCode}/insert", method = RequestMethod.POST)
    public JSONResult insert(@RequestBody BcParam param,@PathVariable String bcCode) {
        param.setBcCode(bcCode);
        LOGGER.info("Class BcController:Method insert:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.insert(param.getBcCode(), param.getParam());
        LOGGER.info("Class BcController:Method insert:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "列表 分页")
    @RequestMapping(value = "/{bcCode}/findPageList", method = RequestMethod.POST)
    public JSONResult findPageList(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findPageList:inParam params: {}", map.toString());
        JSONResult jsonResult = bcService.findPageListNew(map);
        LOGGER.info("Class BcController:Method findPageList:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "列表 分页")
    @RequestMapping(value = "/{bcCode}/findMsterSlvePageList", method = RequestMethod.POST)
    public JSONResult findMsterSlvePageList(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findMsterSlvePageList:inParam params: {}", map.toString());
        JSONResult jsonResult = bcService.findMsterSlvePageList(map);
        LOGGER.info("Class BcController:Method findMsterSlvePageList:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "查看bc pickmap的数据")
    @RequestMapping(value = "/{bcCode}/findPicmapList", method = RequestMethod.POST)
    public JSONResult findPicmapList(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findPicmapList:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.findPicmapList(map);
        LOGGER.info("Class BcController:Method findPicmapList:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "查看bc pickmap的数据 分页")
    @RequestMapping(value = "/{bcCode}/findPicmapPageList", method = RequestMethod.POST)
    public JSONResult findPicmapPageList(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findPicmapPageList:inParam params: {}", map.toString());
        JSONResult jsonResult = bcService.findPicmapPageList(map);
        LOGGER.info("Class BcController:Method findPicmapPageList:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "列表 无分页")
    @RequestMapping(value = "/{bcCode}/findList", method = RequestMethod.POST)
    public JSONResult findList(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findPageList:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.findListNew(map);
        LOGGER.info("Class BcController:Method findPageList:OutParam jsonResult: {}", jsonResult.toString());
        return jsonResult;
    }

    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "根据对应的BCCODE以及条件获取对应的field的字段")
    @RequestMapping(value = "/findListFiled", method = RequestMethod.POST)
    public JSONResult findListFiled(@RequestBody BcParam param) {
        LOGGER.info("Class BcController:Method findListFiled:inParam param: ", param.toString());
        List<Map<String, Object>> list = bcService.findListFiled(param.getBcCode(), null);
        LOGGER.info("Class BcController:Method findListFiled:OutParam list: {}", list);
        return JSONResult.success(list);
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "根据bc名称和对应的id进行获取对应的信息")
    @RequestMapping(value = "/{bcCode}/get", method = RequestMethod.POST)
    public JSONResult get(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method get:inParam map {} ", map);
        JSONResult jsonResult = bcService.get(map);
        LOGGER.info("Class BcController:Method get:OutParam jsonResult: {}", jsonResult);
        return jsonResult;
    }

    /**
     * @param param
     * @return
     */
    @ApiOperation(value = "获取FiledAndFun")
    @RequestMapping(value = "/getFiledAndFun", method = RequestMethod.POST)
    public JSONResult getFiledAndFun(@RequestBody BcParam param) {
        LOGGER.info("Class BcController:Method get:inParam param: {}", param.toString());
        JSONResult jsonResult = bcService.getFiledAndFun(param.getBcCode());
        LOGGER.info("Class BcController:Method get:OutParam jsonResult: {}", jsonResult);
        return jsonResult;
    }

    /**
     * @param map
     * @return
     */
    @ApiOperation(value = "根据数字配置进行计算")
    @RequestMapping(value = "/{bcCode}/getDigital", method = RequestMethod.POST)
    public JSONResult getDigital(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method getDigital:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.getDigital(map);
        LOGGER.info("Class BcController:Method getDigital:OutParam jsonResult: " + jsonResult);
        return jsonResult;
    }

    @ApiOperation(value = "在已有的信息基础上获取pickmap的信息")
    @RequestMapping(value = "/getPikMapPage", method = RequestMethod.POST)
    public JSONResult getPikMapPage(@RequestBody Map<String, Object> map) {
        LOGGER.info("Class BcController:Method getPikMap:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.getBCPickPageMapDetal(map);
        LOGGER.info("Class BcController:Method getPikMap:OutParam jsonResult: " + jsonResult);
        return jsonResult;
    }

    @ApiOperation(value = "在已有的信息基础上获取pickmap的信息")
    @RequestMapping(value = "/getPikMap", method = RequestMethod.POST)
    public JSONResult getPikMap(@RequestBody Map<String, Object> map) {
        LOGGER.info("Class BcController:Method getPikMap:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.getBCPickMapDetal(map);
        LOGGER.info("Class BcController:Method getPikMap:OutParam jsonResult: " + jsonResult);
        return jsonResult;
    }

    @ApiOperation(value = "导出")
    @RequestMapping(value = "/exportAutoFile", method = RequestMethod.GET)
    public JSONResult exportFile(@RequestParam Map<String, Object> map, HttpServletResponse response) {
        JSONResult result;
        try {
            bcService.exportAutoFile(map, response);
            result = JSONResult.success();
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
            result = JSONResult.fail(201, e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "获取PDF")
    @RequestMapping(value = "/getPDF", method = RequestMethod.GET)
    public void getPDF(@RequestParam Map<String, Object> map, HttpServletResponse response) throws Exception {
        bcService.getPDF(map, response);
    }

    @ApiOperation(value = "获取sub的配置bc信息")
    @RequestMapping(value = "/getSUB", method = RequestMethod.POST)
    public JSONResult getSUB(@RequestBody BcParam param) {
        return bcService.getBCSub(param.getBcCode());
    }

    @ApiOperation(value = "数据统计分析")
    @RequestMapping(value = "/{bcCode}/getStatistics", method = RequestMethod.POST)
    public JSONResult getStatistics(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        JSONResult jsonResult;
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        if(!StringUtils.isEmpty(map.get("xAxis").toString()) && !StringUtils.isEmpty(map.get("yAxis").toString())){
            LOGGER.info("Class BcController:Method findDigital:inParam map: {}", JSON.toJSONString(map));
            jsonResult = bcService.getStatistics(map);
        }else {
            jsonResult = JSONResult.fail(201, "参数有误，请检查请正确配置相关参数");
        };
        LOGGER.info("Class BcController:Method findDigital:OutParam jsonResult: " + jsonResult);
        return jsonResult;
    }

    @ApiOperation(value = "数据统计分析")
    @RequestMapping(value = "/{bcCode}/getCount", method = RequestMethod.POST)
    public JSONResult getCount(@RequestBody Map<String, Object> map,@PathVariable String bcCode) {
        map.put(Constants.VALIDATEPARAM.BCCODE,bcCode);
        LOGGER.info("Class BcController:Method findDigital:inParam map: {}", JSON.toJSONString(map));
        JSONResult jsonResult = bcService.getCount(map);
        LOGGER.info("Class BcController:Method findDigital:OutParam jsonResult: " + jsonResult);
        return jsonResult;
    }
}
